package db

import (
	"context"
	"giikin.oder/config"
	"giikin.oder/kitex_gen/com/giikin/enum"
	"giikin.oder/model"
	"log"
)

func GetOrderEtcInfo(ctx context.Context, orderID int64, orderEtcType enum.OrderEtcType) (*model.OrderEtc, error) {
	var orderEtcInfo model.OrderEtc

	err := config.DB.Model(&model.OrderEtc{}).Where("order_id = ?", orderID).Where("type = ?", orderEtcType).First(&orderEtcInfo).Error
	if err != nil {
		log.Println(err)
		return nil, err
	}

	return &orderEtcInfo, nil
}

func AddOrderEtc(ctx context.Context, orderID int64, orderEtcType enum.OrderEtcType, value int64, subData string) error {
	err := config.DB.Create(&model.OrderEtc{
		OrderID: orderID,
		Type:    orderEtcType,
		Value:   value,
		SubData: subData,
	}).Error
	if err != nil {
		log.Println(err)
	}

	return err
}

func UpdateOrderEtc(ctx context.Context, orderID int64, orderEtcType enum.OrderEtcType, value int64, subData string) error {
	err := config.DB.Model(&model.OrderEtc{}).Where("order_id = ?", orderID).Where("type = ?", orderEtcType).Update("value", value).Update("sub_data", subData).Error
	if err != nil {
		log.Println(err)
	}

	return err
}
